import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:xiongan_app_yy/entrys/rp/user_entity.dart';
import 'package:xiongan_app_yy/pages/home/home_list_page.dart';
import 'package:xiongan_app_yy/provider/index_model.dart';

class HomeMostPage extends StatefulWidget {
  List<UserAppmodulemanuslistmanutreedtoNezadatreelist> infos;

  @override
  _HomeMostPageState createState() => _HomeMostPageState();

  HomeMostPage(this.infos);
}

class _HomeMostPageState extends State<HomeMostPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController; //需要定义一个Controller
  List tabs = [];
  List<Widget> widgets = [];

  @override
  void initState() {
    super.initState();
    for (int i = 0; i < widget.infos.length; i++) {
      tabs.add(widget.infos[i].nodeName);
    }

    for (int i = 0; i < widget.infos.length; i++) {
      if (widget.infos.length == 2) {
        widgets.add(HomeListPage(widget.infos[i].nodeName, false));
      } else if (widget.infos.length == 3) {
        if (i == 1) {
          widgets.add(HomeListPage(widget.infos[i].nodeName, true));
        } else {
          widgets.add(HomeListPage(widget.infos[i].nodeName, false));
        }
      } else if (widget.infos.length == 4) {
        if (i == 1 || i == 2) {
          widgets.add(HomeListPage(widget.infos[i].nodeName, true));
        } else {
          widgets.add(HomeListPage(widget.infos[i].nodeName, false));
        }
      } else if (widget.infos.length == 5) {
        if (i == 1 || i == 2 || i == 3) {
          widgets.add(HomeListPage(widget.infos[i].nodeName, true));
        } else {
          widgets.add(HomeListPage(widget.infos[i].nodeName, false));
        }
      }
    }
    _tabController = TabController(length: tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return ScopedModelDescendant<IndexModel>(builder: (context, child, model) {
      _tabController.addListener(() {
        if (tabs[_tabController.index] == "出库预约") {
          model.setIndex(0);
        } else if (tabs[_tabController.index] == "配送预约") {
          model.setIndex(1);
        } else if (tabs[_tabController.index] == "结余退库") {
          model.setIndex(2);
        } else if (tabs[_tabController.index] == "入库预约") {
          model.setIndex(3);
        } else if (tabs[_tabController.index] == "调配预约") {
          model.setIndex(4);
        }
      });
      return Column(
        children: <Widget>[
          Container(
            color: Colors.white,
            child: TabBar(
              labelColor: Colors.blue,
              unselectedLabelColor: Color(0xff646464),
              indicatorColor: Colors.blue,
              indicatorSize: TabBarIndicatorSize.label,
              isScrollable: true,
              tabs: tabs.map((e) => Tab(text: e)).toList(),
              controller: _tabController,
            ),
          ),
          Divider(height: 1, color: Color(0xffeeeeee)),
          Expanded(
              child: TabBarView(
            children: widgets,
            controller: _tabController,
          ))
        ],
      );
    });
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }
}
